package com.koron.css2.systemManage.servlet;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.OrgUserQueryBean;
import com.koron.css2.systemManage.bean.PostUserQueryBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.css2.systemManage.mapper.PostUserMapper;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 岗位人员
 *
 * @author 许海宜
 * 2020年7月31日
 */
public class PostUserSelect implements ServerInterface {

    private static Logger log = LogManager.getLogger(PostUserSelect.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
        try {
            PostUserQueryBean bean = JsonUtils.objectToPojo(req.getData(), PostUserQueryBean.class);
            PostUserMapper mapper = factory.getMapper(PostUserMapper.class, "_default");
            OrgUserMapper userMapper = factory.getMapper(OrgUserMapper.class);
            BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
            if (StringUtils.isBlank(bean.getPostNo())) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请传入岗位。", null);
            }
            // 1. 先从公共库查出所有水司库中拥有这个角色的用户取出来
            List<String> list = mapper.selectUserByPost(bean);
            if (list == null || list.size() == 0) {
                info.setData(new ArrayList<>());
                return info;
            }
            // 2. 查出所有的营业区域
            OrgUserQueryBean userQueryBean = new OrgUserQueryBean();
            userQueryBean.setUserAccounts(list);
            /*List<String> areaIdList = Tools.queryChildrenArea(userQueryBean.getBusinessArea(), userInfo.getCurWaterCode(), businessAreaMapper);
            userQueryBean.setBusinessAreaList(areaIdList);*/
            // 3. 查出当前水司库的所有拥有这个角色的用户
            List<OrgUserBean> users = userMapper.selectByAccounts(userQueryBean);
            // 4. 格式化数据
            List<Map> data = users.stream().map(t -> {
                Map<String, String> map = new HashMap<String, String>();
                map.put("userAccount", t.getAccount());
                map.put("userName", t.getUserName());
                return map;
            }).collect(Collectors.toList());
            info.setData(data);
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription(e.getMessage());
            log.error(e.getMessage(), e);
        }
        return info;
    }

}
